package com.hmy.finance.service.receipt.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.finance.api.bo.core.response.customerBalance.CustomerBalanceAccountInfoResponse;
import com.hmy.finance.bo.request.receipt.InitializeCustomerBalanceAccountRequest;
import com.hmy.finance.bo.response.receipt.InitializeBalanceAccountResponse;
import com.hmy.finance.dal.mapper.ReceivableInventoryMapper;
import com.hmy.finance.dal.mapper.receipt.CustomerBalanceAccountMapper;
import com.hmy.finance.dal.po.receipt.CustomerBalanceAccountPo;
import com.hmy.finance.service.receipt.CustomerBalanceAccountProvideService;
import com.hmy.finance.service.receipt.CustomerBalanceAccountService;
import com.hmy.finance.service.receipt.dto.CustomerBalanceAccountInfoDto;
import com.hmy.finance.service.receipt.dto.customerAccount.CustomerInitialAccountDto;
import com.hmy.finance.service.receipt.rdto.customerAccount.CustomerInitialAccountRdto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author loqel
 * @description 针对表【customer_balance_account(客户余额账户)】的数据库操作Service实现
 * @createDate 2024-04-11 11:33:19
 */
@Slf4j
@Service
public class CustomerBalanceAccountServiceImpl implements CustomerBalanceAccountService {

    @Resource
    private CustomerBalanceAccountMapper customerBalanceAccountMapper;

    @Resource
    private ReceivableInventoryMapper receivableInventoryMapper;

    @Resource
    private CustomerBalanceAccountProvideService customerBalanceAccountProvideService;

    @Override
    public CustomerBalanceAccountInfoResponse queryCustomerBalanceAccountInfo(CustomerBalanceAccountInfoDto customerBalanceAccountInfoDto) {
        log.info("CustomerBalanceAccountService#queryCustomerBalanceAccountInfo request:{}", JSON.toJSON(customerBalanceAccountInfoDto));
        /*查询客户账户信息*/
        LambdaQueryWrapper<CustomerBalanceAccountPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerBalanceAccountPo::getCustomerId, customerBalanceAccountInfoDto.getCustomerId());
        queryWrapper.eq(CustomerBalanceAccountPo::getIsDeleted, 0);
        CustomerBalanceAccountPo customerBalanceAccount = customerBalanceAccountMapper.selectOne(queryWrapper);
        if (Objects.isNull(customerBalanceAccount)) {
            throw new HMYRuntimeException(506800024, new Object[]{customerBalanceAccountInfoDto.getCustomerId()});
        }

        /*查询该客户的总欠款金额*/
        BigDecimal customerContractTotalArrearsAmount = receivableInventoryMapper.selectCustomerContractTotalArrearsAmount(customerBalanceAccountInfoDto.getCustomerId());
        if (Objects.isNull(customerContractTotalArrearsAmount)) {
            customerContractTotalArrearsAmount = BigDecimal.ZERO;//如果为null,目前给0，后续根据业务需求调整是否报错。
        }

        CustomerBalanceAccountInfoResponse response = new CustomerBalanceAccountInfoResponse();
        response.setCustomerId(customerBalanceAccountInfoDto.getCustomerId());
        response.setCustomerName(customerBalanceAccount.getCustomerName());
        response.setCustomerContractTotalArrearsAmount(customerContractTotalArrearsAmount);
        response.setCustomerOffsetAmount(customerBalanceAccountInfoDto.getCustomerOffsetAmount());
        response.setCustomerBalanceAmount(customerBalanceAccount.getBalanceAmount());
        response.setCustomerArrearsAmountAfterOffset(customerContractTotalArrearsAmount.subtract(customerBalanceAccountInfoDto.getCustomerOffsetAmount()));//核销后欠款金额=本次核销金额-核销前欠款金额
        log.info("CustomerBalanceAccountService#queryCustomerBalanceAccountInfo response:{}", JSON.toJSON(response));
        return response;
    }

    @Override
    public InitializeBalanceAccountResponse initializeCustomerBalanceAccount(InitializeCustomerBalanceAccountRequest request) {
        log.info("CustomerBalanceAccountService#initializeCustomerBalanceAccount request:{}", JSONUtil.toJsonStr(request));
        CustomerInitialAccountDto customerInitialAccountDto = new CustomerInitialAccountDto();
        customerInitialAccountDto.setCustomerId(request.getCustomerId());
        customerInitialAccountDto.setCustomerName(request.getCustomerName());
        customerInitialAccountDto.setAmount(request.getInitBalance());

        CustomerInitialAccountRdto customerInitialAccountRdto = customerBalanceAccountProvideService.initialCustomerBalanceAccount(customerInitialAccountDto);
        if (Objects.isNull(customerInitialAccountRdto)) {
            throw new HMYRuntimeException(506400007);
        }
        InitializeBalanceAccountResponse response = new InitializeBalanceAccountResponse();
        response.setBalanceAccountId(customerInitialAccountRdto.getCustomerId());
        response.setSuccess(customerInitialAccountRdto.isSuccess());
        return response;
    }

}




